﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InkFx.WinControl.Core;
using InkFx.WinControl.Utils;
using DA = Temp_20190707_AForm.DockAlignment;
using DTGS = Temp_20190707_AForm.DockTabGroupState;

namespace Temp_20190707_AForm
{
    public class SplitControl : System.Windows.Forms.SplitContainer
    {
        public SplitControl()
        {
            SetStyle(Win32API.ControlStylesUAORSD, true);
            base.SplitterWidth = 3;
            this.SplitterMoving += SplitControlSplitterMoving;
            this.SplitterMoved += SplitControlSplitterMoved;
        }

        public bool IsSplitterMoving { get; private set; }
        private void SplitControlSplitterMoving(object sender, SplitterCancelEventArgs e)
        {
            IsSplitterMoving = true;
            SetCursor(this.Orientation != Orientation.Vertical ? Cursors.HSplit : Cursors.VSplit);
        }
        private void SplitControlSplitterMoved(object sender, SplitterEventArgs e)
        {
            IsSplitterMoving = false;
            SetCursor(null);
        }
        private void SetCursor(System.Windows.Forms.Cursor cursor)
        {
            System.Windows.Forms.Cursor baseCursor = base.Cursor;
            if (baseCursor != null && baseCursor != cursor)
                base.Cursor = cursor;
        }



        public static DockSplitControl FindContainer(SplitterPanel panel)
        {
            if (panel == null || panel.Disposing || panel.IsDisposed) return null;
            var prop = typeof(SplitterPanel).GetProperty("Owner", Win32API.ReflectBindingFlags);
            return prop.GetValue(panel, null) as DockSplitControl;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //e.Graphics.FillRectangle(new SolidBrush(Color.Purple), base.SplitterRectangle);
        }
    }

    public class DockSplitControl : SplitControl
    {
        public DockSplitControl(DockPanel dockPanel)
        {
            //base.Panel1Collapsed = true;
            this.m_DockPanel = dockPanel;
            this.ParentChanged += DockSplitControlParentChanged;
            this.Panel1.ControlAdded += DockSplitControlPanelControlAdded;
            this.Panel2.ControlAdded += DockSplitControlPanelControlAdded;
            this.Panel1.ControlRemoved += DockSplitControlPanelControlRemoved;
            this.Panel2.ControlRemoved += DockSplitControlPanelControlRemoved;
        }


        private void DockSplitControlParentChanged(object sender, EventArgs e)
        {
            var parent = (this.Disposing || this.IsDisposed) ? null : this.Parent;
            if (parent == null || parent.Disposing || parent.IsDisposed) return;
            if (parent is DockPanel) return;
            if (parent is SplitterPanel) return;

            throw new Exception(string.Format("DockSplitControl 只能是 DockPanel 或者 SplitterPanel 的子控件, 而不能是 类型为 '{0}' 的子控件!", parent.GetType().FullName));
        }
        private void DockSplitControlPanelControlAdded(object sender, ControlEventArgs e)
        {
            if (e.Control is DockTabGroup) return;
            if (e.Control is DockSplitControl) return;

            throw new Exception(string.Format("DockSplitControl.Panel 的子控件, 只能是 DockTabGroup 或 DockSplitControl 类型, 而不能是 类型为 '{0}' 的子控件!", e.Control.GetType().FullName));
        }
        private void DockSplitControlPanelControlRemoved(object sender, ControlEventArgs e)
        {
            //this.DoLayout();
        }


        private DockPanel m_DockPanel;
        public DockPanel DockPanel
        {
            get { return m_DockPanel; }
        }
        public SplitterPanel ParentSplitPanel
        {
            get
            {
                var splitPanel = this.Parent as SplitterPanel;
                return splitPanel;
            }
        }
        public DockSplitControl ParentSplitControl
        {
            get
            {
                var parent = this.ParentSplitPanel;
                var splitCtrl = (parent != null) ? parent.Parent as DockSplitControl : null;
                return splitCtrl;
            }
        }


        public virtual bool PanelAllCollapsed { get; private set; }
        public virtual new bool Panel1Collapsed
        {
            get { return base.Panel1Collapsed; }
            set
            {
                PanelAllCollapsed = this.Panel2Collapsed && value;
                base.Panel1Collapsed = value;
            }
        }
        public virtual new bool Panel2Collapsed
        {
            get { return base.Panel2Collapsed; }
            set
            {
                PanelAllCollapsed = this.Panel1Collapsed && value;
                base.Panel2Collapsed = value;
            }
        }




        public void ResetLayout()
        {
            var ctrls1 = this.Panel1.Controls;
            var ctrls2 = this.Panel2.Controls;

            foreach (var ctrl in ctrls1)
                if (ctrl is DockSplitControl) { ((DockSplitControl)ctrl).ResetLayout(); }

            foreach (var ctrl in ctrls2)
                if (ctrl is DockSplitControl) { ((DockSplitControl)ctrl).ResetLayout(); }

            this.DoLayout();
        }
        public void DoLayout()
        {
            if (this.Disposing || this.IsDisposed || this.DockPanel == null) return;
            //if (this == this.DockPanel.CenterSplitControl) return;

            bool hide1, hide2;
            var ctrl1 = this.DockPanel.FindTabGroupOrSplitControl(this.Panel1, out hide1);
            var ctrl2 = this.DockPanel.FindTabGroupOrSplitControl(this.Panel2, out hide2);

            #region  处理 已关闭的 部分

            if ((ctrl1 == null && ctrl2 != null) || (ctrl2 == null && ctrl1!=null))
            {
                #region  执行合并

                var parent = this.Parent;
                var ctrl = ctrl1 ?? ctrl2;
                ctrl.Parent = parent;
                this.Dispose();
                return;

                #endregion
            }

            if (ctrl1 != null && ctrl2 != null)
            {
                #region  执行折叠

                //if (hide1 && hide2)
                //{
                //    var panel = this.ParentSplitPanel;
                //    var splitCtrl = this.ParentSplitControl;
                //    if (splitCtrl != null)
                //    {
                //        if (panel == splitCtrl.Panel1) { splitCtrl.Panel1Collapsed = true; }
                //        else if (panel == splitCtrl.Panel2) { splitCtrl.Panel2Collapsed = true; }
                //    }
                //}
                //else
                //{
                //    this.Panel1Collapsed = hide1;
                //    this.Panel2Collapsed = hide2;
                //}

                this.Panel1Collapsed = hide1;
                this.Panel2Collapsed = hide2;

                #endregion
            }

            #endregion

        }




        public virtual new void Dispose()
        {
            this.ParentChanged -= DockSplitControlParentChanged;
            this.Panel1.ControlAdded -= DockSplitControlPanelControlAdded;
            this.Panel2.ControlAdded -= DockSplitControlPanelControlAdded;
            this.Panel1.ControlRemoved -= DockSplitControlPanelControlRemoved;
            this.Panel2.ControlRemoved -= DockSplitControlPanelControlRemoved;

            var ctrls1 = this.Panel1.Controls;
            var ctrls2 = this.Panel1.Controls;
            if (ctrls1.Count >= 1) ctrls1.Clear();
            if (ctrls2.Count >= 1) ctrls2.Clear();

            this.Parent = null;
            this.m_DockPanel = null;
            base.Dispose();
        }



        public static bool SplitPanelExpand(DockPanel dockPanel, SplitterPanel panel)
        {
            if (panel == null) return false;
            var ctrls = panel.Controls;
            var ctrl = (ctrls.Count >= 1 ? ctrls[0] : null) ?? dockPanel.FindTabGroup(panel);
            if (ctrl is DockTabGroup) return ((DockTabGroup)ctrl).State == DTGS.Expand;
            if (ctrl is DockSplitControl) return SplitControlExpand(dockPanel, (DockSplitControl)ctrl);
            return false;
        }
        public static bool SplitControlExpand(DockPanel dockPanel, DockSplitControl control)
        {
            if (control == null) return false;
            var riveted1 = SplitPanelExpand(dockPanel, control.Panel1);
            if (riveted1) return true;
            var riveted2 = SplitPanelExpand(dockPanel, control.Panel2);
            if (riveted2) return true;
            return false;
        }
    }
}
